package com.xing.springbootdemo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xing.springbootdemo.base.R;
import com.xing.springbootdemo.base.ResultCode;
import com.xing.springbootdemo.domain.Book;
import com.xing.springbootdemo.dto.book.BookPageReqDto;
import com.xing.springbootdemo.dto.book.BookPageRespDto;
import com.xing.springbootdemo.dto.book.BookRespDto;
import com.xing.springbootdemo.service.BookService;
import com.xing.springbootdemo.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * springbootdemo
 * 2021/11/21 11:46
 */
@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {

//    @Autowired
//    @Resource
//    private BookMapper bookMapper;

    @Autowired
    private BookService bookService;

    /**
     * 查询所有书籍
     * @return
     */
    @GetMapping("/findAll")
    private List<Book>  findAll(){
        return bookService.findAll();
    }

    /**
     * 获取当前企业所有的员工列表
     * @return
     */
    @GetMapping("/b")
    public List<Book> getAllBook() {
        return  bookService.findAll();
    }

    @GetMapping("/a")
    public List<Book> getAllBooklist() {
        return  bookService.list();
    }


    /**
     * 根据ID查询书籍
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public R getBookById(Integer id){
        Book book=bookService.getBookById(id);
        return R.data(ResultCode.SUCCESS.getCode(),book,"查询书籍成功");
    }


    /**
     * 获取书籍列表分页
     * @param reqDto
     * @return
     */
    @PostMapping("/bookPage")
    public R<BookPageRespDto> bookPage(@RequestBody @Valid BookPageReqDto reqDto){
        Book book = new Book();
        //bookPage设置当前页为1，每页最多为10
        IPage<Book> bookPage = new Page<Book>()
                //设置当前页码的值为第一页
                .setCurrent(reqDto.getPageNum())
                .setSize(reqDto.getPageSize());

        book.setBookId(reqDto.getBookId());
        book.setAuthor(reqDto.getAuthor());
        book.setName(reqDto.getName());
        book.setPublish(reqDto.getPublish());
        book.setPrice(reqDto.getPrice());

        //bookQueryWrapper查询条件为前端页面传过来的部分参数
        QueryWrapper<Book> bookQueryWrapper =
                new QueryWrapper<>(book);

        bookQueryWrapper.orderByDesc("pubdate");

        IPage<Book> page = bookService.page(bookPage, bookQueryWrapper);
        List<BookRespDto> data = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(page.getRecords())){
            page.getRecords().forEach(p->{
                data.add(this.toBookRespDto(p));
            });
        }
        BookPageRespDto response = new BookPageRespDto();
        response.setData(data);
        response.setPageNum(page.getCurrent());
        response.setPages(page.getPages());
        response.setPageSize(page.getSize());
        response.setTotal(page.getTotal());
        return  R.data(response);

    }

    private  BookRespDto toBookRespDto(Book book){
        BookRespDto bookRespDto = new BookRespDto();
        if(null==book){
            log.info(">>>===当前book为空");
            return bookRespDto;
        }
        bookRespDto.setBookId(book.getBookId());
        bookRespDto.setName(book.getName());
        bookRespDto.setAuthor(book.getAuthor());
        bookRespDto.setPublish(book.getPublish());
        bookRespDto.setPrice(book.getPrice());
        return  bookRespDto;
    }

}
